from django.shortcuts import render, redirect, HttpResponse
from utils.sql_helper import exec_sql
import json


def model_add_class(request):
    ret = {"status": True, "msg": None}
    try:
        cname = request.POST.get("cname")
        exec_sql(f'insert into class (cname) values ("{cname}")', is_modify=True)
    except Exception as e:
        ret["status"] = False
        ret["msg"] = str(e)
    return HttpResponse(json.dumps(ret))


def model_edit_class(request):
    ret = {"status": True, "msg": None}
    try:
        cid = request.POST.get("cid")
        cname = request.POST.get("cname")
        exec_sql(f'update class set cname = "{cname}" where id = {cid}', is_modify=True)
    except Exception as e:
        ret["status"] = False
        ret["msg"] = str(e)
    return HttpResponse(json.dumps(ret))


def model_del_class(request):
    ret = {"status": True, "msg": None}
    try:
        cid = request.POST.get("cid")
        exec_sql(f'delete from class where id = {cid}', is_modify=True)
    except Exception as e:
        ret["status"] = False
        ret["msg"] = str(e)
    return HttpResponse(json.dumps(ret))
